/**
 * JAVACC DEMO 1.0
 */
package com.apache.passport.service.plugins;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.database.constant.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.passport.entity.BlackRoster;
import com.apache.passport.entity.UctUser;
import com.apache.passport.manager.UctUserManager;

/**
 * description:  更改密码,需要旧密码
 */
public class UpdatePwdPlugin implements PluginConnector {
	protected IDao uctUserDao;
	private UctUserManager uctUserManager;
	protected IDao blackRosterDao;

	public void setUctUserDao(IDao uctUserDao) {
		this.uctUserDao = uctUserDao;
	}

	public void setUctUserManager(UctUserManager uctUserManager) {
		this.uctUserManager = uctUserManager;
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see PluginConnector#execute(ParamsVo)
	 */
	@Override
	public Object execute(ParamsVo paramsvo) throws Exception {
		ResultEntity result = new ResultEntity();
		result.setResult("true");

		//登录前首先从数据库中查询下该用户是否存在于黑名单中
		BlackRoster br = (BlackRoster) getBrInfoByIp(paramsvo);

		if (!Validator.isEmpty(br)) {
			result.setEntity("F");
			result.setMessage("账户被冻结,无法修改密码,请联系管理员");
			return result;
		}

		String userId = (String) paramsvo.getParams("userId");
		String userEname = (String) paramsvo.getParams("userEname");
		String userPass = (String) paramsvo.getParams("userPass");
		String newPass = (String) paramsvo.getParams("newPass");
		String mobile = (String) paramsvo.getParams("mobile");
		String email = (String) paramsvo.getParams("email");

		if (Validator.isNull(userEname)) {
			result.setEntity("F");
			result.setMessage("用户名不能为空");
			return result;
		}

		if (Validator.isNull(newPass)) {
			result.setEntity("F");
			result.setMessage("新密码不能为空");
			return result;
		}

		if (Validator.isNull(userPass)) {
			result.setEntity("F");
			result.setMessage("旧密码不能为空");
			return result;
		}

		if (newPass.equals(userPass)) {
			result.setEntity("F");
			result.setMessage("新密码与旧密码相同");
			return result;
		}

		if (Validator.isNull(userId)) {
			if (Validator.isNull(mobile)) {
				result.setEntity("F");
				result.setMessage("手机号不能为空");
				return result;
			}
			if (Validator.isNull(email)) {
				result.setEntity("F");
				result.setMessage("邮箱地址不能为空");
				return result;
			}
		}

		UctUser obj = (UctUser) getInfoByEname(userEname);
		if (!Validator.isEmpty(obj)) {
			if (!userPass.equals(obj.getUserPass())) {
				result.setEntity("F");
				result.setMessage("旧密码不正确");
				return result;
			}

			//因判断与下面的判断,存在执行顺序,故不能放到else中
			if (!Validator.isNull(userId)) {
				if (!userId.equals(obj.getUserId())) {
					result.setEntity("F");
					result.setMessage("定义信息不匹配");
					return result;
				}
			}

			if (Validator.isNull(userId)) {

				if (!email.equals(obj.getEmail())) {
					result.setEntity("F");
					result.setMessage("邮箱与注册邮箱不匹配");
					return result;
				}
				if (!mobile.equals(obj.getMobile())) {
					result.setEntity("F");
					result.setMessage("手机号码与注册手机号码不匹配");
					return result;
				}
			}
			obj.setUserPass(newPass);
			paramsvo.setObj(obj);
			uctUserManager.editInfo(paramsvo);
			result.setEntity("T");
			result.setMessage("密码修改成功");
			return result;
		} else {
			result.setEntity("F");
			result.setMessage("用户不存在");
			return result;
		}
	}

	/**
	 * description:  根据ename查询用户信息
	 * @return
	 * @author Hou Dayu 
	 * @param userEname 
	 * @update 2014-12-19
	 */
	public Object getInfoByEname(String userEname) {
		String sql = uctUserDao.getSql(3);
		MethodParam param = new MethodParam("ByUserEname", "", sql, "com.apache.passport.entity.UctUser");
		param.setParams("userEname", userEname);
		return uctUserDao.selectSingle(param);
	}

	/**
	 * description:  根据IP及用户名获取黑名单
	 * @param vo
	 * @return  
	 * @update 2014-12-24
	 */
	public Object getBrInfoByIp(ParamsVo<UctUser> vo) {
		String remoteIp = (String) vo.getParams("remoteIp");
		String userEname = (String) vo.getParams().get("userEname");
		//只验证用户名
		String sql = blackRosterDao.getSql(4);
		if (!Validator.isNull(remoteIp)) {
			sql += " and brIpaddress :=brIpaddress ";
		}
		MethodParam param = new MethodParam("ByIpOrUserEname", "", sql, BlackRoster.class.getName());
		param.setParams("brIpaddress", remoteIp);
		param.setParams("brAccount", userEname);
		return blackRosterDao.selectSingle(param);
	}

	public IDao getUctUserDao() {
		return uctUserDao;
	}

	public UctUserManager getUctUserManager() {
		return uctUserManager;
	}

	public void setBlackRosterDao(IDao blackRosterDao) {
		this.blackRosterDao = blackRosterDao;
	}
}
